/**
 * 平安数据爬虫
 * author: Kannce
 * createTime: 2020-04-10 09:20:20
 * updateTime: 2020-04-19 03:21:25
 * url:XXX
 **/
var baseUrl = 'http://property.pingan.com'
var timer = 800;
function fetchData({
    provinceCode,
    cityCode,
    page = 0
}) {
    return new Promise(function (reslove) {
        $.getJSON(baseUrl + "/property_insurance/pa18AutoInquiry/queryRepairStoreList.do", {
            provinceCode,
            cityCode,
            page
        }, reslove)
    })
}
function getCityList() {
    var lists = [];
    $("#citySelect").children().each((i, item) => lists.push(item.value))
    return lists;
}
function importExportExcelPackage() {
    var oScript = document.createElement("script");
    oScript.src = "https://cuikangjie.github.io/JsonExportExcel/dist/JsonExportExcel.min.js";
    document.body.appendChild(oScript);

}
function storesDataToExcel(fileName, datas) {
    var options = {};
    var _tips = "数据转Excel中...<br />"
    options.fileName = "理赔网点数据 - " + fileName
    options.datas = [{
        //第一个sheet
        sheetData: datas,
        sheetName: fileName,
        sheetHeader: Object.keys(datas[0])
    }]
    var toExcel = new ExportJsonExcel(options);
    toExcel.saveExcel();
}

(function () {
    const storesData = [];
    var pageIndex = 0;
    var citys = getCityList();
    var loop = async function () {
        if (pageIndex === -1) return;
        const {
            root,
            totalNum
        } = await fetchData({
            provinceCode: 320000,
            cityCode: 320100,
            page: pageIndex
        })

        const {
            partnerInfo_cursor
        } = root;
        partnerInfo_cursor.map(item => storesData.push(item))
        if (Math.ceil(totalNum / 10) === pageIndex) {
            pageIndex = -1;
        } else {
            pageIndex++;
        }
        setTimeout(() => {
            loop();
        }, timer);
    }
    importExportExcelPackage();
    loop();
})()
